import apiClient from "../apiClient";
import { PageQuery } from "@/types/api";

export interface Project {
	id: number;
	name: string;
	url: string;
	createtime: string | null;
	updatetime: string | null;
	status: number;
	status_text: string;
}

export interface ApiResponse<T> {
	status: number;
	message: string;
	time: string;
	data: {
		items: T[];
		total: number;
	};
}

export interface ProjectQuery extends PageQuery {
	name?: string;
	status?: number;
}

export interface ProjectForm
	extends Pick<Project, "name" | "url" | "status"> {}

enum Api {
	ProjectList = "/project/index",
	ProjectAdd = "/project/add",
	ProjectEdit = "/project/edit",
	ProjectDelete = "/project/del",
}

export class ProjectService {
	/**
	 * 获取项目列表
	 */
	async projectList(params: ProjectQuery) {
		return apiClient.get<ApiResponse<Project>>({
			url: Api.ProjectList,
			params,
		});
	}

	/**
	 * 添加项目
	 */
	async projectAdd(data: ProjectForm) {
		return apiClient.post<ApiResponse<void>>({
			url: Api.ProjectAdd,
			data,
		});
	}

	/**
	 * 编辑项目
	 */
	async projectEdit(id: number, data: Partial<ProjectForm>) {
		return apiClient.post<ApiResponse<void>>({
			url: Api.ProjectEdit,
			data: { id, ...data },
		});
	}

	/**
	 * 删除项目
	 */
	async projectDelete(id: number) {
		return apiClient.post<ApiResponse<void>>({
			url: Api.ProjectDelete,
			data: { id },
		});
	}

	/**
	 * 更新项目状态
	 */
	async projectChangeStatus(id: number, status: number) {
		return this.projectEdit(id, { status });
	}
}

export const projectService = new ProjectService();
